<?php

namespace  app\index\controller;
use app\index\model\User as UserModel;
use app\index\model\Profile;
use think\Validate;
use app\index\model\Book;
use think\Controller;



class User extends Controller
{
    public  function  add()
    {       
       $data  = input('post.');
       // 验证birthday是否有效的日期
       $check = Validate::is($data['birthday'],'date');
       if (false === $check) {
           return 'birthday日期格式非法';
       }
       $user  = new UserModel;
       // 数据保存
       $user->save($data);
       return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
      
    }
    
    
    // 批量新增用户数据
    public function addList()
    {
        $user = new UserModel;
        $list = [
            ['nickname' => '张三', 'email' => 'zhanghsan@qq.com', 'birthday' => strtotime('1988-01-15')],
            ['nickname' => '李四', 'email' => 'lisi@qq.com', 'birthday' => strtotime('1990-09-19')],
        ];
        if ($user->saveAll($list)) {
            return '用户批量新增成功';
        } else {
            return $user->getError();
        }
    }
    //UserModel::get 与Db find区别
    //模型的get方法返回的一定是 当前模型对象实例
    //Db类默认返回的数组
    public function read($id='1')
    {
 
        $user=UserModel::get($id);
        
        echo $user->nickname . '<br/>';
        echo $user->email . '<br/>';
        echo $user->birthday . '<br/>';
        echo $user->status . '<br/>';
        echo $user->create_time . '<br/>';
        echo $user->update_time . '<br/>';

    }
    
    public function index(){

        $list=UserModel::scope('email','11@11.com')
        ->all();
        foreach($list as $user){
            echo $user->id . '<br/>';
            echo $user->nickname . '<br/>';
            echo $user->email . '<br/>';
            echo $user->birthday. '<br/>';
            echo $user->status. '<br/>';
            echo '----------------------------------<br/>';
        }
    }
    
    // 更新用户数据
    public function update($id)
    {

        
        
        $user['id']       = (int) $id;
        $user['nickname'] = '刘晨';
        $user['email']    = 'liu21st@gmail.com';
        $result           = UserModel::update($user);
        if (false !== $result) {
            return '更新用户成功';
        } else {
            return $user->getError();
        }
    }
    
   
    
    // 删除用户数据
    public function delete($id)
    {

        $result = UserModel::destroy($id);
        if ($result) {
            return '删除用户成功';
        } else {
            return '删除的用户不存在';
        }
        
    }
    
    
    public function create()
    {
        
      //  return phpinfo();
        
       return view();
    }
    
    
    public  function allRelation(){
        
        $user           = new UserModel;
        $user->name     = 'thinkphp';
      //  $user->password = '123456';
        $user->nickname = '流年T';
        if ($user->save()) {
            
            
            // 写入关联数据

            $profile['truename'] = '刘晨';
            $profile['birthday'] = '1977-03-05';
            $profile['address']  = '中国上海';
            $profile['email']    = 'thinkphp@qq.com';
            
            
            $user->profile()->save($profile);
            return '用户新增成功';
        } else {
            return $user->getError();
        }
           
    }
    
    public function updateR($id)
    {

        
        
        $user = UserModel::get($id);
        $user->books()->where('title', 'ThinkPHP5快速入门')->update(['title' => 'ThinkPHP5开发手册']);
    }
    
    
    public function deleteR($id)
    {
        $user = UserModel::get($id);
        if ($user->delete()) {
            // 删除关联数据
            $user->profile->delete();
            return '用户[ ' . $user->name . ' ]删除成功';
        } else {
            return $user->getError();
        }
        
        
        
    }
    
    
    public function  readR($id){

        $user = UserModel::hasWhere('books', ['title' => 'ThinkPHP5快速入门'])->select();
        dump($user);
    }

    
    public function addBook($id){
    
        $user               = UserModel::get($id);
        $book               = new Book;
    

        $books = [
            ['title' => 'ThinkPHP5快速入门', 'publish_time' => '2016-05-06'],
            ['title' => 'ThinkPHP5开发手册', 'publish_time' => '2016-03-06'],
        ];
        $user->books()->saveAll($books);
    
    
        return '添加Book成功';
    }
    
}






